package com.guomao.iodemos;

import com.guomao.entity.User;

import java.io.*;

public class DemoB {


    public static void main(String[] args) {


        User user = new User("root", 30);


        String objfile = "obj.txt";

//        writeObj(objfile,user);
        readObj(objfile, user);


//        int x = 10;
//        long l = x;
//        int y = (int)l;
    }

    private static void readObj(String objfile, User user) {

        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(objfile));


            int x = 10;//栈区
            Integer y = 20;//堆区

            Object o = ois.readObject();

            ois.readBoolean();
            ois.readByte();
            ois.readChar();

            if (o instanceof String) {
                String s = (String) o;
                System.out.println(s);
            }
            if (o instanceof User) {
                User u = (User) o;
                System.out.println(u);
            }
//            o.getName();

//            System.out.println(u.getName());
//            System.out.println(u.length());

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
        }

    }

    //序列化对象
    private static void writeObj(String objfile, User user) {

        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(objfile));

            oos.writeObject(user);

            oos.flush();

            oos.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
        }
    }

}
